package cn.stylefeng.guns.modular.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.log.BussinessLog;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.modular.system.entity.SysSwitch;
import cn.stylefeng.guns.modular.system.model.params.SysSwitchParam;
import cn.stylefeng.guns.modular.system.model.result.SysSwitchResult;
import cn.stylefeng.guns.modular.system.service.SysSwitchService;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.guns.sys.core.constant.dictmap.UserDict;
import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;


/**
 * 开关设置表控制器
 *
 * @author zhucx
 * @Date 2020-01-02 10:20:36
 */
@Controller
@RequestMapping("/sysSwitch")
public class SysSwitchController extends BaseController {

    private String PREFIX = "/system/sysSwitch";

    @Autowired
    private SysSwitchService sysSwitchService;

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;
    
    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2020-01-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/sysSwitch.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2020-01-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/sysSwitch_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2020-01-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/sysSwitch_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2020-01-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(SysSwitchParam sysSwitchParam) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	SysSwitchParam param = new SysSwitchParam();
    	param.setDeptId(deptId);
    	param.setDeliverId(deliverId);
    	param.setCode(sysSwitchParam.getCode());
    	SysSwitchResult result = sysSwitchService.findBySpec(param,user.getOperateId());
    	if (result!=null) {
    		ResponseData data = new ResponseData();
    		data.setSuccess(false);
    		data.setMessage("编码已存在!");
    		return data;
		}
    	
    	sysSwitchParam.setCreateName(user.getName());
    	sysSwitchParam.setCreateAccount(user.getAccount());
    	sysSwitchParam.setOperation("Y");
    	sysSwitchParam.setDeptId(deptId);
    	sysSwitchParam.setDeliverId(deliverId);
    	if (deptId!=null) {
        	Dept Dept = deptService.getBydeptId(deptId);
        	if (Dept!=null) {
            	sysSwitchParam.setDeptName(Dept.getSimpleName());
    		}
		}
        this.sysSwitchService.add(sysSwitchParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2020-01-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(SysSwitchParam sysSwitchParam) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	SysSwitchParam param = new SysSwitchParam();
    	param.setId(sysSwitchParam.getId());
    	param.setDeptId(deptId);
    	param.setDeliverId(deliverId);
    	param.setCode(sysSwitchParam.getCode());
    	SysSwitchResult result = sysSwitchService.findBySpec(param,user.getOperateId());
    	if (result!=null) {
    		ResponseData data = new ResponseData();
    		data.setSuccess(false);
    		data.setMessage("编码已存在!");
    		return data;
		}
    	
    	sysSwitchParam.setDeptId(deptId);
    	sysSwitchParam.setDeliverId(userService.getDeliverId());
    	if (deptId!=null) {
        	Dept Dept = deptService.getBydeptId(deptId);
        	if (Dept!=null) {
            	sysSwitchParam.setDeptName(Dept.getSimpleName());
    		}
		}
        this.sysSwitchService.update(sysSwitchParam);
        return ResponseData.success();
    }

    /**
     * 开启
     * @author zhucx
     */
    @RequestMapping("/unfreeze")
    @ResponseBody
    public ResponseData unfreeze(@RequestParam Integer id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        SysSwitch detail = this.sysSwitchService.getById(id);
        SysSwitchParam sysSwitchParam = new SysSwitchParam();
        detail.setOperation("Y");
        ToolUtil.copyProperties(detail,sysSwitchParam);
        this.sysSwitchService.update(sysSwitchParam);
        return ResponseData.success();
    }

    /**
     * 关闭
     *
     * @author zhucx
     */
    @RequestMapping("/freeze")
    @ResponseBody
    public ResponseData freeze(@RequestParam Integer id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        SysSwitch detail = this.sysSwitchService.getById(id);
        SysSwitchParam sysSwitchParam = new SysSwitchParam();
        detail.setOperation("N");
        ToolUtil.copyProperties(detail,sysSwitchParam);
        this.sysSwitchService.update(sysSwitchParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2020-01-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(SysSwitchParam sysSwitchParam) {
        this.sysSwitchService.delete(sysSwitchParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2020-01-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(SysSwitchParam sysSwitchParam) {
        SysSwitch detail = this.sysSwitchService.getById(sysSwitchParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2020-01-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(SysSwitchParam sysSwitchParam,String condition ) {
    	User user = userService.getcurrentUser();
    	sysSwitchParam.setDeliverId(user.getDeliverId());
    	sysSwitchParam.setDeptId(user.getDeptId());
        return this.sysSwitchService.findPageBySpec(sysSwitchParam,condition,user.getOperateId());
    }

}


